﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    public partial class AuditTableBFGive_List : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                Bind();
            }
        }

        private void Bind()
        {
            int? batchNo = null;
            if (txtBatchNo.Text.Trim().Length > 0) { batchNo = Convert.ToInt32(txtBatchNo.Text); }
            bool? isBF = null, isGive = null;
            if (ddlBF.SelectedValue != "0")
            {
                isBF = ddlBF.SelectedValue == "2";
            }
            if (ddlGive.SelectedValue != "0")
            {
                isGive = ddlGive.SelectedValue == "2";
            }
            int pageCount = 0, recordCount = 0;
            DataTable dt = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetBFGive(batchNo, txtProjectSN.Text.Trim(), txtProjectName.Text.Trim(), txtContractName.Text.Trim(), txtCode.Text.Trim(), isGive, isBF, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dt;
            rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            //TextBox txtGiveAmount = e.Item.FindControl("txtGiveAmount") as TextBox;
            //txtGiveAmount.Attributes.Add("onchange", "setGiveAmount('" + drv["ID"].ToString() + "',this.value);");
            //TextBox txtBFAmount = e.Item.FindControl("txtBFAmount") as TextBox;
            //txtBFAmount.Attributes.Add("onchange", "setBFAmount('" + drv["ID"].ToString() + "',this.value);");
            //TextBox txtGiveDate = e.Item.FindControl("txtGiveDate") as TextBox;
            //txtGiveDate.Attributes.Add("onpropertychange", "setGiveDate('" + drv["ID"].ToString() + "',this.value);");
            //TextBox txtBFDate = e.Item.FindControl("txtBFDate") as TextBox;
            //txtBFDate.Attributes.Add("onpropertychange", "setBFDate('" + drv["ID"].ToString() + "',this.value);");
            //txtGiveAmount.Text = string.Format("{0:0.00}", drv["GiveAmount"]);
            //if (txtGiveAmount.Text == "0.00") { txtGiveAmount.Text = ""; }
            //txtBFAmount.Text = string.Format("{0:0.00}", drv["BFAmount"]);
            //if (txtBFAmount.Text == "0.00") { txtBFAmount.Text = ""; }
            //txtGiveDate.Text = string.Format("{0:yyyy-MM-dd}", drv["GiveDate"]);
            //txtBFDate.Text = string.Format("{0:yyyy-MM-dd}", drv["BFDate"]);

            TextBox txtGiveAmount = e.Item.FindControl("txtGiveAmount") as TextBox;
            TextBox txtBFAmount = e.Item.FindControl("txtBFAmount") as TextBox;
            TextBox txtGiveDate = e.Item.FindControl("txtGiveDate") as TextBox;
            TextBox txtBFDate = e.Item.FindControl("txtBFDate") as TextBox;

            txtGiveAmount.Attributes.Add("onchange", "setValue('" + drv["ID"].ToString() + "','" + txtGiveAmount.ClientID + "','" + txtGiveDate.ClientID + "','" + txtBFAmount.ClientID + "','" + txtBFDate.ClientID + "');");
            txtBFAmount.Attributes.Add("onchange", "setValue('" + drv["ID"].ToString() + "','" + txtGiveAmount.ClientID + "','" + txtGiveDate.ClientID + "','" + txtBFAmount.ClientID + "','" + txtBFDate.ClientID + "');");
            txtGiveDate.Attributes.Add("onpropertychange", "setValue('" + drv["ID"].ToString() + "','" + txtGiveAmount.ClientID + "','" + txtGiveDate.ClientID + "','" + txtBFAmount.ClientID + "','" + txtBFDate.ClientID + "');");
            txtBFDate.Attributes.Add("onpropertychange", "setValue('" + drv["ID"].ToString() + "','" + txtGiveAmount.ClientID + "','" + txtGiveDate.ClientID + "','" + txtBFAmount.ClientID + "','" + txtBFDate.ClientID + "');");
            
            txtGiveAmount.Text = string.Format("{0:0.00}", drv["GiveAmount"]);
            if (txtGiveAmount.Text == "0.00") { txtGiveAmount.Text = ""; }
            txtBFAmount.Text = string.Format("{0:0.00}", drv["BFAmount"]);
            if (txtBFAmount.Text == "0.00") { txtBFAmount.Text = ""; }
            txtGiveDate.Text = string.Format("{0:yyyy-MM-dd}", drv["GiveDate"]);
            txtBFDate.Text = string.Format("{0:yyyy-MM-dd}", drv["BFDate"]);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string res = hidResult.Value;
                if (res != "")
                {
                    string[] arrRes = res.Split(',');
                    List<Bu_AuditTable> lsUpdate = new List<Bu_AuditTable>();
                    Bu_AuditTable item = null;
                    foreach (string arrItem in arrRes)
                    {
                        item = new Bu_AuditTable();
                        string[] arrTemp = arrItem.Split('|');
                        item.ID = arrTemp[0];
                        if (arrTemp[1] != "")
                        {
                            item.GiveAmount = Convert.ToDecimal(arrTemp[1]);
                        }
                        if (arrTemp[2] != "")
                        {
                            item.GiveDate = Convert.ToDateTime(arrTemp[2]);
                        }
                        if (arrTemp[3] != "")
                        {
                            item.BFAmount = Convert.ToDecimal(arrTemp[3]);
                        }
                        if (arrTemp[4] != "")
                        {
                            item.BFDate = Convert.ToDateTime(arrTemp[4]);
                        }
                        lsUpdate.Add(item);
                    }
                    new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).UpdateBFGive(lsUpdate);
                    ScriptHelper.Window.Alert("保存成功！");
                    Bind();
                }
            }
            catch(Exception ex)
            {
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！"+ex.Message);
            }
        }
    }
}